/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.AplicacaoInjecaoController;
import Controller.RelatorioController;
import VO.VOAplicacaoInjecao;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tiago
 */

public class ImprimirAplicaçãoInjeçãoView extends javax.swing.JDialog {
    AplicacaoInjecaoController aiControl = new AplicacaoInjecaoController();
    
    public ImprimirAplicaçãoInjeçãoView(java.awt.Frame parent, boolean modal) {
        super(parent, false);
        initComponents();
        setLocationRelativeTo( null );
    }

    public void carregarTabelaAplicacaoInjecao(ArrayList<VOAplicacaoInjecao> lista){
        String [] colunas = new String[]{"Código da Aplicação", "Código da Receita", "Código do Usuário", "Usuário", "Braço", "Data", "Situação"};
        DefaultTableModel modelo = new DefaultTableModel(null, colunas){
            public boolean isCellEditable(int row, int col){
                return false;
            }
        };
        tbImprimirAplicacaoInjecao.setModel(modelo);;
        try{
            for(VOAplicacaoInjecao apVO:lista){
                String[] linha = new String[]{apVO.getCodAplicacao(),
                                              apVO.getCodReceita(),
                                              apVO.getCodUsuario(),
                                              apVO.getNomeUsuario(),
                                              apVO.getBraco(),
                                              apVO.getDataAplicacao(),
                                              apVO.getSituacao()};
                modelo.addRow(linha);
            }
        } catch (Exception e){
            System.out.println("ImprimirInjecaoView - Erro: " + e.getMessage().toString());
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnImprimirAplicacaoInjecao = new javax.swing.JPanel();
        lbImprimirAplicacaoInjecaoUsuario = new javax.swing.JLabel();
        tfImprimirAplicacaoInjecaoUsuario = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbImprimirAplicacaoInjecao = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btImprimir = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Aplicação de Injeção");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        pnImprimirAplicacaoInjecao.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        lbImprimirAplicacaoInjecaoUsuario.setText("Usuário:");

        tfImprimirAplicacaoInjecaoUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfImprimirAplicacaoInjecaoUsuarioKeyReleased(evt);
            }
        });

        tbImprimirAplicacaoInjecao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbImprimirAplicacaoInjecao);

        javax.swing.GroupLayout pnImprimirAplicacaoInjecaoLayout = new javax.swing.GroupLayout(pnImprimirAplicacaoInjecao);
        pnImprimirAplicacaoInjecao.setLayout(pnImprimirAplicacaoInjecaoLayout);
        pnImprimirAplicacaoInjecaoLayout.setHorizontalGroup(
            pnImprimirAplicacaoInjecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnImprimirAplicacaoInjecaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnImprimirAplicacaoInjecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 703, Short.MAX_VALUE)
                    .addGroup(pnImprimirAplicacaoInjecaoLayout.createSequentialGroup()
                        .addComponent(lbImprimirAplicacaoInjecaoUsuario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfImprimirAplicacaoInjecaoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnImprimirAplicacaoInjecaoLayout.setVerticalGroup(
            pnImprimirAplicacaoInjecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnImprimirAplicacaoInjecaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnImprimirAplicacaoInjecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbImprimirAplicacaoInjecaoUsuario)
                    .addComponent(tfImprimirAplicacaoInjecaoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btImprimir.setText("Imprimir");
        btImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btImprimirActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(btImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(btCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnImprimirAplicacaoInjecao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnImprimirAplicacaoInjecao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfImprimirAplicacaoInjecaoUsuarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfImprimirAplicacaoInjecaoUsuarioKeyReleased
        ArrayList<VOAplicacaoInjecao> lista = aiControl.pesquisarPendentePorUsuario(tfImprimirAplicacaoInjecaoUsuario.getText());
            carregarTabelaAplicacaoInjecao(lista);
    }//GEN-LAST:event_tfImprimirAplicacaoInjecaoUsuarioKeyReleased

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        ArrayList<VOAplicacaoInjecao> lista = aiControl.obterAplicacaoPendente();
        carregarTabelaAplicacaoInjecao(lista);
    }//GEN-LAST:event_formWindowActivated

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btImprimirActionPerformed
        if(tbImprimirAplicacaoInjecao.getSelectedRow() == -1){
            JOptionPane.showMessageDialog(null, "Selecione o registro que deseja imprimir!");
        }
        else{
            int linha = tbImprimirAplicacaoInjecao.getSelectedRow();
            try {
                RelatorioController rCtrl = new RelatorioController();
                rCtrl.mostrarRelatorio(tbImprimirAplicacaoInjecao.getModel().getValueAt(linha, 2).toString());
            } catch (IOException ex) {
                Logger.getLogger(PrincipalView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(PrincipalView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btImprimirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ImprimirAplicaçãoInjeçãoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ImprimirAplicaçãoInjeçãoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ImprimirAplicaçãoInjeçãoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ImprimirAplicaçãoInjeçãoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ImprimirAplicaçãoInjeçãoView dialog = new ImprimirAplicaçãoInjeçãoView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btImprimir;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbImprimirAplicacaoInjecaoUsuario;
    private javax.swing.JPanel pnImprimirAplicacaoInjecao;
    private javax.swing.JTable tbImprimirAplicacaoInjecao;
    private javax.swing.JTextField tfImprimirAplicacaoInjecaoUsuario;
    // End of variables declaration//GEN-END:variables
}
